//
//  PageViewController.m
//  xBooks
//
//  Created by wicresoft on 6/23/11.
//  Copyright 2011 wicresoft. All rights reserved.
//

#import "PageViewController.h"

#define FONT_SIZE_MAX   20

@implementation PageViewController
@synthesize text;
@synthesize textLabel;
@synthesize pageInfoLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [text release];
    [textLabel release];
    [pageInfoLabel release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //
    totalPages = 0;
    currentPage = 0;
    
    //
    textLabel.numberOfLines = 0;
    
    //
    if (!text) {
        // 从文件里加载文本串
        [self loadString];
        
        // 计算文本串的大小尺寸
        CGSize totalTextSize = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE_MAX]
                                constrainedToSize:CGSizeMake(textLabel.frame.size.width, CGFLOAT_MAX)
                                    lineBreakMode:UILineBreakModeWordWrap];
        
        // 如果一页就能显示完，直接显示所有文本串即可。
        if (totalTextSize.height < textLabel.frame.size.height) {
            textLabel.text = text;
        }
        else {
            // 计算理想状态下的页面数量和每页所显示的字符数量，只是拿来作为参考值用而已！
            NSUInteger textLength = [text length];
            referTotalPages = (int)totalTextSize.height/(int)textLabel.frame.size.height+1;
            referCharatersPerPage = textLength/referTotalPages;
            
            // 申请最终保存页面NSRange信息的数组缓冲区
            int maxPages = referTotalPages;
            rangeOfPages = (NSRange *)malloc(referTotalPages*sizeof(NSRange));
            memset(rangeOfPages, 0x0, referTotalPages*sizeof(NSRange));
            
            // 页面索引
            int page = 0;
            
            for (NSUInteger location = 0; location < textLength; ) {
                // 先计算临界点（尺寸刚刚超过UILabel尺寸时的文本串）
                NSRange range = NSMakeRange(location, referCharatersPerPage);
                
                // reach end of text ?
                NSString *pageText;
                CGSize pageTextSize;
                
                while (range.location + range.length < textLength) {
                    pageText = [text substringWithRange:range];
                    
                    pageTextSize = [pageText sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE_MAX]
                                        constrainedToSize:CGSizeMake(textLabel.frame.size.width, CGFLOAT_MAX)
                                            lineBreakMode:UILineBreakModeWordWrap];
                    
                    if (pageTextSize.height > textLabel.frame.size.height) {
                        break;
                    }
                    else {
                        range.length += referCharatersPerPage;
                    }
                }
                
                if (range.location + range.length >= textLength) {
                    range.length = textLength - range.location;
                }
                
                // 然后一个个缩短字符串的长度，当缩短后的字符串尺寸小于textLabel的尺寸时即为满足
                while (range.length > 0) {
                    pageText = [text substringWithRange:range];
                    
                    pageTextSize = [pageText sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE_MAX]
                                        constrainedToSize:CGSizeMake(textLabel.frame.size.width, CGFLOAT_MAX)
                                            lineBreakMode:UILineBreakModeWordWrap];
                    
                    if (pageTextSize.height <= textLabel.frame.size.height) {
                        range.length = [pageText length];
                        break;
                    }
                    else {
                        range.length -= 2;
                    }
                }
                
                // 得到一个页面的显示范围
                if (page >= maxPages) {
                    maxPages += 10;
                    rangeOfPages = (NSRange *)realloc(rangeOfPages, maxPages*sizeof(NSRange));
                }
                rangeOfPages[page++] = range;
                
                // 更新游标
                location += range.length;
            }
            
            // 获取最终页面数量
            totalPages = page;
            
            // 更新UILabel内容
            textLabel.text = [text substringWithRange:rangeOfPages[currentPage]];
        }
    }
    
    // 显示当前页面进度信息，格式为："8/100"
    pageInfoLabel.text = [NSString stringWithFormat:@"%d/%d", currentPage+1, totalPages];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

// 上一页
- (IBAction)actionPrevious:(id)sender {
    if (currentPage > 0) {
        currentPage--;
        
        NSRange range = rangeOfPages[currentPage];
        NSString *pageText = [text substringWithRange:range];
        
        textLabel.text = pageText;
        
        //
        pageInfoLabel.text = [NSString stringWithFormat:@"%d/%d", currentPage+1, totalPages];
    }
}

////////////////////////////////////////////////////////////////////////////////////////
// 下一页
- (IBAction)actionNext:(id)sender {
    if (currentPage < totalPages-1) {
        currentPage++;
        
        NSRange range = rangeOfPages[currentPage];
        NSString *pageText = [text substringWithRange:range];
        
        textLabel.text = pageText;
        
        //
        pageInfoLabel.text = [NSString stringWithFormat:@"%d/%d", currentPage+1, totalPages];
    }
}

-(void)loadString
{
    NSString *filePath =[[NSBundle mainBundle] pathForResource:@"page" ofType:@"txt"];
   
//    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"txt"];  
//    NSData *myData = [NSData dataWithContentsOfFile:filePath];  
//    if (myData) {  
//        // do something useful  
//    }
//    NSData *data = [NSData dataWithContentsOfFile:filePath];
//    if (data) {
//        text =[[NSstring alloc] in]
//    }
    
    
    self.text =[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

}

- (IBAction)bactAction
{
    [self dismissModalViewControllerAnimated:YES];
}

//-(void)readInfo {  
//    isread = YES;  
//    NSArray *paths=NSSearchPathForDirectoriesInDomains（NSDocumentDirectory,NSUserDomainMask,YES);  
//    NSString *path=[paths objectAtIndex:0];  
//    NSString *filename=[path stringByAppendingPathComponent:@"user.plist"];  
//    NSMutableDictionary *dict=[[NSMutableDictionary alloc] initWithContentsOfFile:filename];  
//    username = [dict objectForKey:@"username"];  
//    password = [dict objectForKey:@"password"];  
//    number = [dict objectForKey:@"number"];  
//    
//}  
//-(void)writeInfo {  
//    NSData *data=[[NSMutableDictionary alloc] init];  
//    [data setValue:username forKey:@"username"];  
//    [data setValue:password forKey:@"password"];  
//    [data setValue:number forKey:@"number"];  
//    
//    
//    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);  
//    NSString *path=[paths objectAtIndex:0];  
//    NSString *filename=[path stringByAppendingPathComponent:@"user.plist"];      
//    
//    [data writeToFile:filename  atomically:YES];  
//    [data release];  
//}  

//NSString *filePath = [[NSBundle mainBundle] pathForResource:@"HelpDoc" ofType:@"htm"];  
//NSData *htmlData = [NSData dataWithContentsOfFile:filePath];  
//if (htmlData) {  
//    [webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@"http://iphoneincubator.com"]];  
//}  

@end
